package com.neuedu.web;

import com.neuedu.pojo.Userinfo;
import com.neuedu.service.UserinfoServiceImpl;
import com.neuedu.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "UserRegServlet",urlPatterns = "/user/reg")
public class UserRegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String email = request.getParameter("email");
        String nickname = request.getParameter("username");
        String password = request.getParameter("pass");
        Date regTime = new Date();
        // 封装信息
        Userinfo userinfo = new Userinfo();
        userinfo.setEmail(email);
        userinfo.setNickname(nickname);
        userinfo.setPassword(MD5Utils.getPwd(password));// 加密
        userinfo.setRegtime(regTime);
        //调service完成注册（插入）
        UserinfoServiceImpl userinfoService = new UserinfoServiceImpl();
        int num = userinfoService.userReg(userinfo);
        if (num != 0){
            // 跳转到登录页面
//            request.getRequestDispatcher("/WEB-INF/jsp/user/login.jsp").forward(request,response);
            response.sendRedirect(request.getContextPath()+"/go/login");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
